package com.tianquan.aspect;

/**
 * @author starsea
 * @date 2024-09-10 上午9:34
 */

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.aspect
 * @Author: tianquan
 * @CreateTime: 2024-09-10 09:34
 * @Description: 学习切面
 * @Version: 1.0
 */
@Aspect
@Component
public class testAspect {

    @Pointcut(" execution(*com.tianquan.Mvc.TestController.test(..) ) && @annotation(com.tianquan.aspect.Test) ")
    public void test() {
    }

    @Around("test()")
    public void JoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {

        // 获取方法签名
        Signature signature = joinPoint.getSignature();
        System.out.println(signature);

        // 获取方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        System.out.println(method);

        // 获取注解里的值
        Test annotation = method.getAnnotation(Test.class);
        String value = annotation.value();
        System.out.println(value);

        // 获取返回类型
        Class<?> returnType = methodSignature.getReturnType();
        System.out.println(returnType);

        // 获取方法名
        String methodName = signature.getName();
        System.out.println(methodName);

        // 获取方法所属类
        Class<?> declaringType = signature.getDeclaringType();
        System.out.println(declaringType);

        // 获取方法所属类名
        String declaringTypeName = signature.getDeclaringTypeName();
        System.out.println(declaringTypeName);

        SourceLocation sourceLocation = joinPoint.getSourceLocation();
        System.out.println(sourceLocation);

        // 获取当前对象
        Object aThis = joinPoint.getThis();
        System.out.println(aThis);

        // 获取目标对象
        Object target = joinPoint.getTarget();
        System.out.println(target);

        // 获取注解信息
        String kind = joinPoint.getKind();
        System.out.println(kind);

        // 获取参数
        String string = joinPoint.getArgs().toString();
        System.out.println(string);

        // 执行目标方法
        Object proceed = joinPoint.proceed();

        System.out.println("执行完了切面");
    }

}
